import AppConstants from "../framework/scripts/AppConstants"
import EventMgr from "../framework/scripts/manager/EventMgr"
import { SdkEvent } from "../framework/scripts/sdk/SdkConstants"
import SdkMgr from "../framework/scripts/sdk/SdkMgr"
import SdkUtil from "../framework/scripts/sdk/SdkUtil"
import UIBase from "../framework/scripts/view/UIBase"
import UIMgr from "../framework/scripts/view/UIMgr"
import Tip from "./core/Tip"
import Wait from "./core/Wait"


export default class UISdk extends UIBase {
    public skinPath: string = 'UISdk'
    public uiShowStyle: number = AppConstants.uiShowStyle.FadeIn


    protected onInitDone(): void {
        EventMgr.on(SdkEvent.Error, (tip: string) => Tip.show(tip), this)

        EventMgr.on(SdkEvent.RewardVideoShowPrepare, this.onRewardVideoShowPrepare, this)
        EventMgr.on(SdkEvent.RewardVideoShowing, this.onRewardVideoShowing, this)
        EventMgr.on(SdkEvent.RewardVideoShowOver, this.onRewardVideoShowOver, this)
    }

    protected onDestroy(): void {
        EventMgr.clear(this)
    }

    protected onButtonClick(button: cc.Node) {
        switch (button.name) {
            case 'BtnBack':
                UIMgr.back()
                break
            case 'BtnRewardVideo':
                cc.log('点击了 激励视频 按钮')
                SdkMgr.inst.rewardVideoShow((flag: boolean) => {
                    cc.log(`flag:${flag}`)
                })
                break
        }
    }


    private onRewardVideoShowPrepare() {
        Wait.show()
    }

    private onRewardVideoShowing() {
        Wait.hide()

        // 暂停音乐音效(oppo 不能暂停, 不然从激励视频返回时会闪屏)
        if (!SdkUtil.isOppo) {
            cc.game.pause()
        }
    }

    private onRewardVideoShowOver() {
        Wait.hide()
        
        if (!SdkUtil.isOppo) {
            cc.game.resume()
        }
    }
}
